﻿using System;
using System.IO;

namespace MuNet
{
    public static class MuFso
    {

        /// <summary>
        /// 检测指定目录是否存在
        /// </summary>
        /// <param name="path">目录的绝对路径</param>
        /// <returns></returns>
        public static bool IsFolder(string path)
        {
            return Directory.Exists(path);
        }

        /// <summary>
        /// 检测指定文件是否存在
        /// </summary>
        /// <param name="path">文件的绝对路径</param>
        /// <returns></returns>
        public static bool IsFile(string path)
        {
            return File.Exists(path);
        }

        /// <summary>
        /// 获取指定目录中的文件列表
        /// </summary>
        /// <param name="path">指定目录的绝对路径</param>
        /// <returns></returns>
        public static string[] GetFiles(string path)
        {
            if (IsFolder(path))
            {
                return Directory.GetFiles(path);
            }
            else
            {
                throw new DirectoryNotFoundException(path);
            }
        }

        /// <summary>
        /// 搜索指定目录及子目录中文件列表
        /// </summary>
        /// <param name="path">指定目录的绝对路径</param>
        /// <param name="pattern">模式字符串，"*"代表0或N个字符，"?"代表1个字符。</param>
        /// <param name="schild">是否搜索子目录</param>
        /// <returns></returns>
        public static string[] GetFiles(string path, string pattern, bool schild)
        {
            if (!IsFolder(path))
            {
                throw new DirectoryNotFoundException(path);
            }
            try
            {
                if (schild)
                {
                    return Directory.GetFiles(path, pattern, SearchOption.AllDirectories);
                }
                else
                {
                    return Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly);
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取指定目录中所有子目录列表
        /// </summary>
        /// <param name="path">指定目录的绝对路径</param>
        /// <returns></returns>
        public static string[] GetFolders(string path)
        {
            try
            {
                return Directory.GetDirectories(path);
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 搜索指定目录及子目录中所有子目录列表
        /// </summary>
        /// <param name="path">指定目录的绝对路径</param>
        /// <param name="pattern">模式字符串，"*"代表0或N个字符，"?"代表1个字符。</param>
        /// <param name="schild">是否搜索子目录</param>
        /// <returns></returns>
        public static string[] GetFolders(string path, string pattern, bool schild)
        {
            try
            {
                if (schild)
                {
                    return Directory.GetDirectories(path, pattern, SearchOption.AllDirectories);
                }
                else
                {
                    return Directory.GetDirectories(path, pattern, SearchOption.TopDirectoryOnly);
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 检测指定目录是否为空
        /// </summary>
        /// <param name="path">指定目录的绝对路径</param>
        /// <returns></returns>
        public static bool IsEmptyFolder(string path)
        {
            try
            {
                string[] files = GetFiles(path);
                if (files.Length > 0)
                {
                    return false;
                }
                string[] folders = GetFolders(path);
                if (folders.Length > 0)
                {
                    return false;
                }
                return true;
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 检测指定目录中是否存在指定的文件
        /// </summary>
        /// <param name="path">指定目录的绝对路径</param>
        /// <param name="pattern">模式字符串，"*"代表0或N个字符，"?"代表1个字符。</param>
        /// <param name="schild">是否搜索子目录</param>
        /// <returns></returns>
        public static bool Find(string path, string pattern, bool schild)
        {
            try
            {
                string[] files = GetFiles(path, pattern, schild);
                if (files.Length == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 检测指定目录中是否存在指定的文件
        /// </summary>
        /// <param name="path">指定目录的绝对路径</param>
        /// <param name="pattern">模式字符串，"*"代表0或N个字符，"?"代表1个字符。</param>
        /// <returns></returns>
        public static bool Find(string path, string pattern)
        {
            return Find(path, pattern, false);
        }

        public static bool CreateFolder(string path)
        {
            try
            {
                if (Directory.Exists(path))
                {
                    return true;
                }
                else
                {
                    Directory.CreateDirectory(path);
                    return true;
                }
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
